package cn.intotw.pp.server.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.Channel;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Chenxiang
 * @Description: 连接管理类，封装对连接的一系列管理
 * @create 2018/8/27 15:28
 */
public class ChannelGroups {
    private static Logger log=LoggerFactory.getLogger(ChannelGroups.class);
    private static ConcurrentHashMap<Long,Channel> channelConcurrentHashMap =new ConcurrentHashMap<Long, Channel>();
    private static final int MAX_SIZE=10000;
    public static void addChannel(Long channelId,Channel channel) throws IllegalAccessException {
        if(channelConcurrentHashMap.size()>MAX_SIZE)
        {
            throw new IllegalAccessException();
        }
        log.debug("addChannel:"+channelId+" "+channel.hashCode());
        channelConcurrentHashMap.put(channelId,channel);
    }
    public static Channel removeChannel(Channel channel)
    {
        log.debug("removeChannel: "+channel.hashCode());
        return channelConcurrentHashMap.remove(channel);
    }
    public static Channel getChannel(Long channelId)
    {
        log.debug("getChannel:"+channelId);
        return channelConcurrentHashMap.get(channelId);
    }
    public static void listChannel()
    {
        Set s=channelConcurrentHashMap.entrySet();
        Iterator it=s.iterator();
        while(it.hasNext())
        {
            Map.Entry<Long,Channel> c= (Map.Entry<Long,Channel>) it.next();
            log.info(c.getKey()+" "+c.getValue().hashCode());
        }
    }
    public static int size()
    {
        return channelConcurrentHashMap.size();
    }
}